package com.controller.TransactionController;

import com.entity.Transaction;
import com.mapper.TransactionMapper;
import com.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
public class TransactionController {
    @Autowired
    private TransactionMapper transactionMapper;

    //通过userId查询交易记录
    @GetMapping("/transactionsList/{userId}")
    private List<Transaction> selectByUserId(@PathVariable int userId){
        return transactionMapper.selectByUserId(userId);
    }
    //通过userId查询账单条目并且条目的类别id替换为类别name
    @GetMapping("/transactionsList1/{id}")
    public List<Transaction> getTransactionsWithCategoryNames(@PathVariable int id) {
        return transactionMapper.selectTransactionsWithCategoryNames(id);
    }
    //增加账单条目
    @PostMapping("/transactionsAdd")
    public int AddTransactions(@RequestBody Transaction t){

        int result = transactionMapper.insert(t);
        return result;
    }
    //通过transl的id查询那一条的信息
    @GetMapping("/transactionsById/{id}")
    public Transaction getTransactionById(@PathVariable int id){
        return transactionMapper.getTransactionWithCategoryNameById(id);
    }

    //修改transaction
    @PutMapping("/transactionsUpdate")
    public boolean updateTransaction(@RequestBody Transaction transaction) {
        System.out.println(transaction.toString());
        int updatedRows = transactionMapper.updateTransaction(transaction);
        return updatedRows > 0;
    }

    //删除

    @DeleteMapping("/transactions/{id}")
    public int deleteTransaction(@PathVariable("id") Long transactionId) {
         return transactionMapper.deleteTransactionById(transactionId);

    }

    //通过分类id查询账单信息
    @GetMapping("/transactionsByCategory/{id}")
    public List<Transaction> transactionsByCategory(@PathVariable long id){
       return transactionMapper.getTransactionsByCategory(id);
    }
}
